from django.shortcuts import render
from django.http import HttpResponse
from django.views import View

import json

# from .models import *

# Create your views here.

# class RegisterView(View):
#     def get(self,request):
#         name = request.POST.get('name')
#         password = request.POST.get('password')
#         repassword = request.POST.get('repassword')
#         user = User.objects.filter(name = name).first()
#         if user:
#             return HttpResponse(json.dumps({'msg':'该用户已存在','code':400}))
#         else:
#             if password==repassword:
#                 User.objects.create(name = name,
#                                     password = password)
#                 return HttpResponse(json.dumps({'msg':'注册成功','code':200}))
#             else:
#                 return HttpResponse(json.dumps({'msg':'两次密码不一致','code':400}))


from rest_framework.mixins import (
    ListModelMixin,
    CreateModelMixin,
    DestroyModelMixin,
    RetrieveModelMixin,
    UpdateModelMixin,
)
from rest_framework.generics import GenericAPIView
# from rest_framework.views import APIView

from rest_framework.response import Response
from rest_framework.views import APIView
from .models import *
from .serializer import GoodsSerializer


# class CateApi(ListModelMixin,CreateModelMixin,GenericAPIView):
#     """
#     分类序列化展示
#     反序列化添加
#     """
#     queryset = Cate.objects.all()
#     serializer_class = CateSerializer
#
#     def get(self,request,*args,**kwargs):
#         return self.list(request,*args,**kwargs)
#     def post(self,request,*args,**kwargs):
#         return self.create(request,*args,**kwargs)

class UrLogin(APIView):
    def get(self, request):
        # 接收参数
        username = request.GET.get('username', None)
        password = request.GET.get('password', None)

        name = User.objects.filter(user_name=username,user_password=password).first()
        if name:
            return Response({'code':200,'message':'登陆成功'})
        else:
            return Response({'code':404,'message':'用户名或密码错误'})

class GoodApi(ListModelMixin,CreateModelMixin,GenericAPIView):
    """
    分类序列化展示
    反序列化添加
    """
    queryset = Good.objects.all()
    serializer_class = GoodsSerializer

    def get(self,request,*args,**kwargs):
        return self.list(request,*args,**kwargs)
    def post(self,request,*args,**kwargs):
        return self.create(request,*args,**kwargs)

